<template>
  <div style="height: 100vh; display: flex; align-items: center; justify-content: center; background-color: #ccc">
    <div style="display: flex; background-color: white; width: 50%; border-radius: 5px; overflow: hidden">
      <div style="flex: 1">
        <img src="@/assets/register.png" alt="" style="width: 100%">
      </div>
      <div style="flex: 1; display: flex; align-items: center; justify-content: center">
        <el-form :model="user" style="width: 80%" :rules="rules" ref="registerRef">
          <div style="font-size: 20px; font-weight: bold; text-align: center; margin-bottom: 20px">欢迎注册项目管理系统</div>
          <el-form-item prop="name">
            <el-input prefix-icon="el-icon-user-solid" size="medium" placeholder="请输入真实姓名" v-model="user.name"></el-input>
          </el-form-item>
          <el-form-item prop="username">
            <el-input prefix-icon="el-icon-user" size="medium" placeholder="请输入电话号码" v-model="user.username" maxlength="11"
                      oninput="value=value.replace(/[^\d]/g,'')"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input prefix-icon="el-icon-lock" size="medium" show-password placeholder="请输入密码" v-model="user.password"></el-input>
          </el-form-item>
          <el-form-item prop="confirmPass">
            <el-input prefix-icon="el-icon-lock" size="medium" show-password placeholder="请确认密码" v-model="user.confirmPass"></el-input>
          </el-form-item>
          <el-form-item prop="role">
            <el-radio-group v-model="user.role">
              <el-radio label="学生"></el-radio>
              <el-radio label="指导老师"></el-radio>
              <el-radio label="评审专家"></el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item prop="classname" v-if="user.role === '学生'">
            <el-select
                v-model="user.classname"
                placeholder="请选择班级"
                style="width: 100%"
                clearable
            >
              <el-option
                  v-for="item in classes"
                  :key="item.id"
                  :label="item.classname"
                  :value="item.classname"
              />
            </el-select>
          </el-form-item>
          <!-- 校验码输入（老师/专家可见） -->
          <el-form-item prop="verificationCode" v-if="user.role === '指导老师' || user.role === '评审专家'">
            <el-input
                prefix-icon="el-icon-key"
                size="medium"
                placeholder="请输入校验码"
                v-model="verificationCode"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" style="width: 100%;background-color: #6e77f2;" @click="register">注 册</el-button>
          </el-form-item>
          <div style="display: flex">
            <div style="flex: 1">已经有账号了？请 <span style="color: #6e77f2; cursor: pointer" @click="$router.push('/login')">登录</span></div>
          </div>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "Register",
  data() {
    // 两次密码校验
    const validatePassword = (rule, confirmPass, callback) => {
      if (confirmPass === '') {
        callback(new Error('请确认密码'))
      } else if (confirmPass !== this.user.password) {
        callback(new Error('两次输入的密码不一致'))
      } else {
        callback()
      }
    }

    // 班级选择校验
    const validateClassname = (rule, value, callback) => {
      if (this.user.role === '学生' && !value) {
        callback(new Error('请选择班级'))
      } else {
        callback()
      }
    }


    // 手机号校验
    const validatePhone = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请输入电话号码'));
      } else if (!/^1[3-9]\d{9}$/.test(value)) {
        callback(new Error('电话号码必须是11位数字'));
      } else {
        callback();
      }
    };
    return {
      classes: [],
      verificationCode: '',
      user: {
        username: '',
        password: '',
        confirmPass: '',
        classname: '',
        name: '',
        role: '',
        verificationCode: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入真实姓名', trigger: 'blur' }
        ],
        username: [
          { required: true, message: '请输入账号', trigger: 'blur' },
          { validator: validatePhone, trigger: 'blur' } // 添加手机号校验规则
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
        ],
        confirmPass: [
          { validator: validatePassword, trigger: 'blur' }
        ],
        role: [
          { required: true, message: '请选择角色', trigger: 'change' }
        ],
        classname: [
          { validator: validateClassname, trigger: 'change' }
        ],

      }
    }
  },
  created() {
    this.getClasses()
  },
  methods: {
    async getClasses() {
      try {
        const res = await this.$request.get('/project/classList')
        if (res.code === '200') {
          this.classes = res.data
        }
      } catch (error) {
        console.error('获取班级列表失败:', error)
      }
    },
    register() {
      this.$refs.registerRef.validate(valid => {
        if (valid) {
          // 提取验证码并从提交数据中移除

          this.$request.post(
              `/register?verificationCode=${encodeURIComponent(this.verificationCode)}`,
              this.user
          ).then(res => {
            if (res.code === '200') {
              this.$router.push('/login')
              this.user.verificationCode = ''
              this.$message.success('注册成功')
            } else {
              this.$message.error(res.msg)
            }
          })
        }
      })
    }
  }
}
</script>

<style scoped>

</style>